//===============================================================================
// Microsoft patterns & practices
// Web Client Software Factory
//-------------------------------------------------------------------------------
// Copyright (C) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//-------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Xml;

namespace WCSFContrib.PageFlow.Xml.Configuration
{
    public class ObjectTypesConfigurationElement : ConfigurationElement
    {
        private const string NodePersistProviderXPath = "statePersistenceProvider";
        private const string NodePageFlowTypeXPath = "pageFlowType";
        private const string NodeObjectTypesXPath = "objectTypes";
        private Dictionary<string, StatePersistenceProviderSettings> _statePersistenceCollection = new Dictionary<string, StatePersistenceProviderSettings>();
        private Dictionary<string, PageFlowTypeSettings> _pageFlowTypesCollection = new Dictionary<string, PageFlowTypeSettings>();


        public ObjectTypesConfigurationElement()
        {
        }

        public Dictionary<string, StatePersistenceProviderSettings> StatePersistenceCollection
        {
            get { return _statePersistenceCollection; }
        }

        public Dictionary<string, PageFlowTypeSettings> PageFlowTypesCollection
        {
            get { return _pageFlowTypesCollection; }
        }
	

        protected override void DeserializeElement(System.Xml.XmlReader reader, bool serializeCollectionKey)
        {
            XmlDocument doc = new XmlDocument();
            doc.InnerXml = reader.ReadOuterXml();
            LoadViewManagementObjects(doc);
        }

        private void LoadViewManagementObjects(XmlNode configNode)
        {
            //Get the configured IViewManager object types
            ObjectTypeSettings typedObject;
            foreach (XmlNode objectTypeNode in configNode.SelectSingleNode(NodeObjectTypesXPath).ChildNodes)
            {
                switch (objectTypeNode.LocalName)
                {
                    //case NodeILayoutManagerXPath:
                    //    typedObject = new ObjectTypeSettings(objectTypeNode);
                    //    _iLayoutManagerCollection.Add(typedObject.Name, typedObject);
                    //    break;
                    //case NodeIViewManagerXPath:
                    //    typedObject = new ObjectTypeSettings(objectTypeNode);
                    //    _iViewManagerCollection.Add(typedObject.Name, typedObject);
                    //    if (IsDefault(objectTypeNode))
                    //        _defaultViewManager = typedObject;
                    //    break;
                    //case NodeStateXPath:
                    //    typedObject = new ObjectTypeSettings(objectTypeNode);
                    //    _stateCollection.Add(typedObject.Name, typedObject);
                    //    if (IsDefault(objectTypeNode))
                    //        _defaultState = typedObject;
                    //    break;
                    //case NodeControllerXpath:
                    //    typedObject = new ObjectTypeSettings(objectTypeNode);
                    //    _controllerCollection.Add(typedObject.Name, typedObject);
                    //    break;
                    case NodePersistProviderXPath:
                        typedObject = new StatePersistenceProviderSettings(objectTypeNode);
                        _statePersistenceCollection.Add(typedObject.Name, (StatePersistenceProviderSettings)typedObject);
                        //if (IsDefault(objectTypeNode))
                        //    _defaultStatePersistence = (StatePersistenceProviderSettings)typedObject;
                        break;

                    case NodePageFlowTypeXPath:
                        typedObject = new PageFlowTypeSettings(objectTypeNode);
                        _pageFlowTypesCollection.Add(typedObject.Name, (PageFlowTypeSettings)typedObject);
                        break;
                }
            }
        }

        //private bool IsDefault(XmlNode objectTypeNode)
        //{
        //    XmlAttribute defaultAttribute = objectTypeNode.Attributes["default"];
        //    if (defaultAttribute != null && defaultAttribute.Value == "true")
        //        return true;

        //    return false;
        //}
    }
}
